﻿using SHKF.B2C.Model;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Dto.WorkingCenter;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Entity.WorkingCenter;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;  

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.Shop
{
    /// <summary>
    /// frmShopEdit.xaml 的交互逻辑
    /// </summary>
    public partial class frmShopEdit : BasePage
    {
        public frmShopEdit()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

        }

        private async void btnSave_Click(object sender, RoutedEventArgs e)
        {
            ShopEntity shop = (ShopEntity)this.DataContext;
            long shopSN = 0;
            if (!long.TryParse(shop.ShopSN.ToString(), out shopSN) || shopSN < 0)
            {
                MessageBox.Show("门店SN不能为空，且只能正数");
                return;
            }

            //保存门店信息
            ShopDto dto = await GL.HttpClientManage.ShopSave(shop);
            if (dto.error != null)
            {
                MessageBox.Show(dto.error.msg);
                return;
            }
            this.DataContext = dto.Data;
             
            List<ShopMapEntity> list = new List<ShopMapEntity>();

            //保存美团闪购
            ShopMapEntity shopMapEntity = CreateMapEntity(this.tabMtsg, PlatformEnum.Mtsg);
            if (shopMapEntity.app_poi_code > 0)
            {
                list.Add(shopMapEntity);
            }

            //饿了么
            ShopMapEntity mapElm = CreateMapEntity(this.tabEleme, PlatformEnum.Eleme);
            if (mapElm.app_poi_code > 0)
            {
                list.Add(shopMapEntity);
            }

            //美团配送
            ShopMapEntity mapMtps = CreateMapEntity(this.tabMtps, PlatformEnum.Mtps);
            if (mapMtps.app_poi_code > 0)
            {
                list.Add(mapMtps);
            }

            //有赞云
            ShopMapEntity mapYzy = CreateMapEntity(this.tabYouZanYun, PlatformEnum.YouZanYun);
            if (mapYzy.app_poi_code > 0)
            {
                list.Add(mapYzy);
            }

            DataDto dataDto =  await GL.HttpClientManage.ShopMapSaveBatch(list);
            if (dataDto.error != null)
            {
                MessageBox.Show(dto.error.msg);
            }

            //保存映射表
            List < ShopRelatedEntity > list2 = this.dgvShopRelated.ItemsSource as List<ShopRelatedEntity>; 
            if(list2!= null && list2.Count > 0)
            {
                foreach (ShopRelatedEntity related in list2)
                {
                    related.ShopVSN = shop.ShopSN;
                }

                await GL.HttpClientManage.SaveBatchShopRelated(list2);
            }
         

            MessageBox.Show("保存成功");

            this.LoadData();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            base.GoBack(null);
        }

        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        { 
            LoadData();
        }

        private ShopMapEntity CreateMapEntity(StackPanel sender, string platformEnum)
        {
            ShopEntity shop = (ShopEntity)this.DataContext;
            ShopMapEntity shopEntity = (ShopMapEntity)sender.DataContext; 
            shopEntity.CompanySN = shop.CompanySN;
            shopEntity.ShopSN = shop.ShopSN;
            shopEntity.Platform = platformEnum;
            return shopEntity;
        }

        //加载数据
        private async void LoadData()
        {
            ShopEntity shopEntity = (ShopEntity)this.DataContext;　　
            if (shopEntity.SN > 0)
            {
                ShopDto shop = await GL.HttpClientManage.ShopGet(shopEntity.SN);　
                this.DataContext = shop.Data;　
                if (shop.error == null && shop.Data != null)
                {
                    List<ShopMapEntity> Maps = shop.Maps;　

                    //美团闪购
                    ShopMapEntity shopMtsg = Maps.Find(ww => ww.Platform == PlatformEnum.Mtsg);
                    if (shopMtsg == null)
                    {
                        shopMtsg = new ShopMapEntity();
                        shopMtsg.CategoryName = "未分类";
                    }
                    this.tabMtsg.DataContext = shopMtsg;

                    //饿了么
                    ShopMapEntity showEleme = Maps.Find(ww => ww.Platform == PlatformEnum.Eleme);
                    if (showEleme == null)
                    {
                        showEleme = new ShopMapEntity();
                        showEleme.CompanySN = shopEntity.CompanySN;
                    }
                    this.tabEleme.DataContext = showEleme;

                    //美团配送 
                    ShopMapEntity shopMtps = Maps.Find(ww => ww.Platform == PlatformEnum.Mtps);
                    if (shopMtps == null)
                    {
                        shopMtps = new ShopMapEntity();
                        shopMtps.CompanySN = shopEntity.CompanySN;
                    }
                    this.tabMtps.DataContext = shopMtps;

                    //有赞云
                    ShopMapEntity showYzy = Maps.Find(ww => ww.Platform == PlatformEnum.YouZanYun);
                    if (showYzy == null)
                    {
                        showYzy = new ShopMapEntity();
                        showYzy.CompanySN = shopEntity.CompanySN;
                    }
                    this.tabYouZanYun.DataContext = showYzy;

                    //加载分仓数据
                    this.LoadShopRelateData(shop.Data.ShopSN);
                }
                else
                {
                    InitPlatData();
                }
            }
            else
            {
                InitPlatData();
            }
           

            this.txtShopSN.IsReadOnly = shopEntity.SN > 0;
        }

        private void InitPlatData()
        {
            //美团闪购
            this.tabMtsg.DataContext = new ShopMapEntity();

            //饿了么
            this.tabEleme.DataContext = new ShopMapEntity();

            //美团配送 
            this.tabMtps.DataContext = new ShopMapEntity();

            //有赞云
            this.tabYouZanYun.DataContext = new ShopMapEntity(); 

        }
        private void btnFindShopYzy_Click(object sender, RoutedEventArgs e)
        {
            ShopMapEntity shopMap = this.tabYouZanYun.DataContext as ShopMapEntity;
            ShopEntity shopEntity = (ShopEntity)this.DataContext;
            frmFindShopYzy frm = new frmFindShopYzy();
            frm.CompanySN = shopEntity.CompanySN;
            if (frm.ShowDialog() == true)
            {
                txtYzyShopSN.Text = frm.app_poi_code.ToString();
            }
        }

        #region 分仓映射


        /// <summary>
        /// 加载分仓数据
        /// </summary>
        private async void LoadShopRelateData(int ShopVSN)
        {
            ShopRelatedListDto dto = await GL.HttpClientManage.QueryShopRelatedList(ShopVSN);

            if( dto.error == null)
            {
                this.dgvShopRelated.ItemsSource = dto.data;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }

        private void btnShopRelatedAdd_Click(object sender, RoutedEventArgs e)
        {
            ShopEntity shopEntity = (ShopEntity)this.DataContext;
            if( shopEntity != null && shopEntity.SN > 0 )
            {
                List<ShopRelatedEntity> data = this.dgvShopRelated.ItemsSource as List<ShopRelatedEntity>;
                int count = 0;
                if (data != null)
                {
                    count = data.Count + 1;
                }

                ShopRelatedEntity info = new ShopRelatedEntity();
                info.ShopVSN = int.Parse(this.txtShopSN.Text);
                info.OrderNumber = count;

                frmShopRelatedEdit win = new frmShopRelatedEdit();
                win.DataContext = info;
                if (win.ShowDialog() == true)
                {
                    LoadShopRelateData(info.ShopVSN);
                }
            }
            else
            {
                MessageBox.Show("请先保存虚拟ShopSN");
            }
           
        }

        private void btnShopRelatedEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvShopRelated.SelectedItems.Count > 0)
            {
                ShopRelatedEntity info = (ShopRelatedEntity)this.dgvShopRelated.SelectedValue; 
                frmShopRelatedEdit win = new frmShopRelatedEdit();
                win.DataContext = info.Clone();
                if ( win.ShowDialog() == true )
                {
                    LoadShopRelateData(info.ShopVSN);
                }
            }
        }

        private async void btnShopRelatedDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("请确认是否要删除", "删除提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                ShopRelatedEntity info = (ShopRelatedEntity)this.dgvShopRelated.SelectedValue;
                DataDto dto = await GL.HttpClientManage.DeleteShopRelated(info.SN);
                if (dto.error == null)
                {
                    this.LoadData();
                }
            }
        }
 
        private void btnShopRelatedRefaush_Click(object sender, RoutedEventArgs e)
        {
            ShopEntity shopEntity = (ShopEntity)this.DataContext;
            int ShopVSN = shopEntity.ShopSN;

            LoadShopRelateData(ShopVSN);
        }

        #endregion
    }
}
